﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 小小次朗点餐系统
{
    public partial class userLogin : Form
    {
        public userLogin()
        {
            InitializeComponent();
        }

        public static string suserName = "";
        public static string spassWord = "";
        private void button1_Click(object sender, EventArgs e)
        {

            suserName = login_phone.Text;
            spassWord = login_password.Text;
            //非空验证
            if (suserName == "" || spassWord == "")
            {
                MessageBox.Show("用户名密码不能为空");
            }
            else
            {

                Accounts.accounts = login_phone.Text;

                //用户名存在验证
                //查询数据库

                //判断为用户还是商家,密码正确验证

                if (login_genre_emptor.Checked == true)
                {
                    //用户
                    string sql = string.Format($"select * from [User] where Unumber='{login_phone.Text}'");
                    DataSet ds = DBHelper.SelectByAdapter(sql);
                    DataTable dt = ds.Tables[0];

                    //Accounts.ID = (int)dt.Rows[0][0];

                    if (dt.Rows.Count > 0)
                    {
                        if (login_password.Text == dt.Rows[0]["pwd"].ToString())
                        {

                            Accounts.ID = (int)dt.Rows[0][0];
                            MessageBox.Show("登录成功！");
                            usergn1 frm3 = new usergn1();
                            this.Visible = false;
                            frm3.ShowDialog();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("密码错误！");
                            return;
                        }

                    }

                   // MessageBox.Show("该账号未注册为用户！");
                    return;
                }

                else
                {
                    string sql = string.Format($"select * from Seller where Senumber='{login_phone.Text}'");
                    DataSet ds = DBHelper.SelectByAdapter(sql);
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count > 0)
                    {

                        Accounts.ID = (int)dt.Rows[0][0];
                        if (login_password.Text == dt.Rows[0]["pwd"].ToString())
                        {
                            MessageBox.Show("登录成功！");
                            Form6 frm6 = new Form6();
                            this.Visible = false;
                            frm6.ShowDialog();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("密码错误！");
                            return;
                        }
                       
                   
                    }
                    //MessageBox.Show("该账号未注册为商家！");
                    return;
                }
            }


        }
    

        private void login_enroll_Click(object sender, EventArgs e)
        {
            Accounts.logon = "0";
            Enroll form2 = new Enroll();
            form2.ShowDialog();
        }

        private void userLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rlt = MessageBox.Show("确认退出系统吗？", "提示：", MessageBoxButtons.YesNo);
            if (rlt == DialogResult.Yes)
            {
                System.Environment.Exit(0);
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {
            Accounts.logon = "1";//把静态类值改成1，方便注册窗体Form2区分是注册还是找回密码
            //跳转到注册窗体
            Enroll form2 = new Enroll();
            form2.ShowDialog();
        }
    }
}
